/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.setup.trac.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.setup.trac.pojo.wrappers.UtentiView;
import com.setup.trac.service.SearchService;
import com.setup.trac.util.TracSetupUtil;

/** 
 *
 * @author edoardo
 */
@SuppressWarnings("deprecation")
public class LoginFormController extends SimpleFormController  {
    private SearchService searchService;
    private TracSetupUtil tracSetupUtil;
    private String sql;
    private String adminView ;
    /**
     * Mappatura tra ruolo dell'utente e user home da caricare
     */
    private Map<String, String> rolesPageMapping;

    public Map<String, String> getRolesPageMapping() {
        return rolesPageMapping;
    }

    public void setRolesPageMapping(Map<String, String> rolesPageMapping) {
        this.rolesPageMapping = rolesPageMapping;
    }

    public LoginFormController() {
        //Initialize controller properties here or 
        //in the Web Application Context
        //setCommandClass(MyCommand.class);
        //setCommandName("MyCommandName");
        //setSuccessView("successView");
        //setFormView("formView");
    }
    
    

    @Override
    public ModelAndView onSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors) throws Exception {
        UtentiView loginForm = (UtentiView) command;
        String md5 = getTracSetupUtil().md5(loginForm.getUtenti().getUsername() + loginForm.getUtenti().getPassword());
        String username = loginForm.getUtenti().getUsername();
        loginForm = getSearchService().findUtente(username,md5);
        HttpSession session = request.getSession(false);
        session.setAttribute("loginForm", loginForm);
        if(loginForm.getUtenti().getPrimoAccesso()){
        	ModelAndView mav = new ModelAndView("redirect:primoAccesso.do");
        	return mav;
        }
        if (getRolesPageMapping() != null) {
/*            String successView = getRolesPageMapping().get(loginForm);
            if (successView != null) {
                setSuccessView(successView);
            }
 * 
 */
        }
        ModelAndView modelAndView = new ModelAndView(getSuccessView());
        if(loginForm.getUtenti().getProfilo().getUtenteAmministratore()){
        	modelAndView = new ModelAndView(getAdminView());
        }
        request.getSession().setAttribute("nextPage", "/search.do");
        request.getSession().setAttribute("loginForm", loginForm);

        return modelAndView;

    }

    public SearchService getSearchService() {
        return searchService;
    }

    public void setSearchService(SearchService searchService) {
        this.searchService = searchService;
    }

 
    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
    /*
    @Override
    protected ModelAndView onSubmit(
    HttpServletRequest request,
    HttpServletResponse response,
    Object command,
    BindException errors) throws Exception {
    ModelAndView mv = new ModelAndView(getSuccessView());
    //Do something...
    return mv;
    }
     */



    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public TracSetupUtil getTracSetupUtil() {
        return tracSetupUtil;
    }

    public void setTracSetupUtil(TracSetupUtil tracSetupUtil) {
        this.tracSetupUtil = tracSetupUtil;
    }

	public String getAdminView() {
		return adminView;
	}

	public void setAdminView(String adminView) {
		this.adminView = adminView;
	}
}
